/**
 * 
 */
package com.buss.core.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.buss.base.model.Branch;
import com.buss.base.service.BranchService;
import com.buss.core.dic.MenuType;
import com.buss.core.dic.RecordStatus;
import com.buss.core.model.Sysmenu;
import com.buss.core.model.User;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.SysmenuService;
import com.buss.core.service.UserService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;

/**
 * 主页Controller
 * @author 高小勇
 * 创建时间:2011-5-3
 * wcms2.0
 */
@Controller
@RequestMapping("/admin")
public class MainFrameController extends BaseController {

	/**用户操作*/
	@Autowired
	private UserService userService;
	
	/**系统菜单操作*/
	@Autowired
	private SysmenuService sysmenuService;
	
	/**店铺服务*/
	@Autowired
	private BranchService branchService;
	
	/**
	 * 登录页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/loginpage")
	public ModelAndView loginPage(HttpServletRequest request){
		ModelAndView view = new ModelAndView("login");
		return view;
	}
	
	/**
	 * 用户注销
	 * @param request
	 * @return
	 */
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request){
		request.getSession().removeAttribute(SysStaticParam.SESSION_USER);
		return new ModelAndView(new RedirectView(request.getContextPath() + "/admin/loginpage.do"));
	}
	
	/**
	 * 登录Controller
	 * @param userId	用户编码
	 * @param password	登录密码
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(@RequestParam String companyId, @RequestParam String userId,@RequestParam String password,HttpServletRequest request){
		if(StringUtil.isEmpty(companyId)){
			request.setAttribute("message", "单位编码不能为空!");
		}
		if(StringUtil.isEmpty(userId)){
			request.setAttribute("message", "用户名不能为空!");
			return loginPage(request);
		}
		if(StringUtil.isEmpty(password)){
			request.setAttribute("message", "用户密码不能为空!");
			return loginPage(request);
		}
		User user = userService.loadUser(companyId,userId);
		if(user == null){
			request.setAttribute("message", "该用户信息不存在，请核实!");
			return loginPage(request);
		}
		if(user.isDisabled()){
			request.setAttribute("message", "该用户已经停用，不能登录系统，请与管理员联系!");
			return loginPage(request);
		}
		if(!user.getUserPass().equals(password)){
			request.setAttribute("message", "用户密码不正确，请核实!");
			return loginPage(request);
		}
		
		/**存放登陆信息*/
		request.getSession().setAttribute(SysStaticParam.SESSION_USER, user);
		
		if(user.getRoles().size() < 1 && !user.isSysAdmin()){
			request.getSession().removeAttribute(SysStaticParam.SESSION_USER);
			request.setAttribute("message", "用户还没有设置角色信息，请与管理员联系!");
			return loginPage(request);
		}
		ResultList<Branch> rs = branchService.queryBranchByRole(this.getUserRoleIds(request), RecordStatus.normal, null);
		if((rs.getData() == null || rs.getData().size() == 0) && !user.isSysAdmin()){
			request.getSession().removeAttribute(SysStaticParam.SESSION_USER);
			request.setAttribute("message", "没有设置店铺权限，请与管理员联系!");
			return loginPage(request);
		}
		/**存放用户当前的店铺信息*/
		request.getSession().setAttribute(SysStaticParam.SESSION_BRANCH, rs.getData().size() > 0 ? rs.getData().get(0) : null);
		return new ModelAndView(new RedirectView(request.getContextPath() + "/admin/mainframe.do"));
	}
	
	@RequestMapping("/mainframe")
	public ModelAndView mainPage(HttpServletRequest request){		
		ModelAndView view = new ModelAndView("mainframe");	
		
		ResultList<Sysmenu> sysmenus = null;
		User user = getUser(request);
		if(!user.isSysAdmin()){
			sysmenus = sysmenuService.querySysMenuByRole(getUserRoleIds(request), 0L, MenuType.menu, false, null);
		}else{
			sysmenus = sysmenuService.querySysMenu(0L, MenuType.menu,false);			
		}
		view.addObject("branchName",getBranchName(request));
		view.addObject("menus", sysmenus.getData());
		return view;
	}

	@Override
	public ModelAndView listPage(@RequestParam(required=false) String menuid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ModelAndView editPage(String type, Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> delete(Long[] ids) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
